<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="代理商订单" prop="dlUserId">
        <el-select filterable v-model="queryParams.dlUserId" placeholder="请选择代理">
          <el-option v-for="item in daililist" :key="item.userId" :label="item.userName"
            :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input v-model="queryParams.orderNumber" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单类型" prop="orderChannel">
        <el-select v-model="queryParams.orderChannel" placeholder="下单类型" clearable>
          <el-option v-for="dict in dict.type.xiadanleixing" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="门店名字" prop="shopName">
        <el-input v-model="queryParams.shopName" placeholder="请输入门店名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="门店联系人" prop="shopPerson" label-width="100px">
                <el-input v-model="queryParams.shopPerson" placeholder="请输入门店联系人" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="门店联系人电话" prop="shopPhone" label-width="110px">
                <el-input v-model="queryParams.shopPhone" placeholder="请输入门店联系人电话" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
      <el-form-item label="商品名字" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入商品名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="购买数量" prop="productCount">
                <el-input v-model="queryParams.productCount" placeholder="请输入购买数量" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="单价" prop="productPrice">
                <el-input v-model="queryParams.productPrice" placeholder="请输入单价" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
      <!-- <el-form-item label="提成" prop="productCommission">
                <el-input v-model="queryParams.productCommission" placeholder="请输入提成" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="总价" prop="totalAmount">
                <el-input v-model="queryParams.totalAmount" placeholder="请输入总价" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
      <!-- <el-form-item label="支付时间" prop="payTime">
                <el-date-picker clearable v-model="queryParams.payTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择支付时间">
                </el-date-picker>
            </el-form-item> -->
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="支付状态" clearable>
          <el-option v-for="dict in zf" :key="dict.name" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="发货类型" prop="deliveryLogisticsType">
        <el-select v-model="queryParams.deliveryLogisticsType" placeholder="支付状态" clearable>
          <el-option v-for="dict in zflx" :key="dict.name" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="收货人" prop="deliveryRecipientName">
        <el-input v-model="queryParams.deliveryRecipientName" placeholder="请输入收货人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收货人电话" prop="deliveryRecipientPhone" label-width="100px">
        <el-input v-model="queryParams.deliveryRecipientPhone" placeholder="请输入收货人电话" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发货状态" prop="deliveryStatus">
        <el-select v-model="queryParams.deliveryStatus" placeholder="发货状态" clearable>
          <el-option v-for="dict in fhstatus" :key="dict.name" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="快递编号" prop="deliveryNumber">
        <el-input v-model="queryParams.deliveryNumber" placeholder="请输入快递编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商品名字" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入商品名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="创建人身份" prop="createUserRole">
        <el-select v-model="queryParams.createUserRole" placeholder="请选择创建人身份" clearable>
          <el-option v-for="dict in jslist" :key="dict.id" :label="dict.roleName" :value="dict.roleKey" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['orders:edit']">修改</el-button>
            </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['orders:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNumber" width="180" />
      <el-table-column label="下单类型" align="center" prop="orderChannel" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xiadanleixing" :value="scope.row.orderChannel" />
        </template>
      </el-table-column>
      <el-table-column label="门店名字" align="center" prop="shopName" width="120" />
      <el-table-column label="门店类型名字" align="center" prop="shopTypeName" width="120" />
      <el-table-column label="门店联系人" align="center" prop="shopPerson" width="120" />
      <el-table-column label="门店联系人电话" align="center" prop="shopPhone" width="120" />
      <el-table-column label="商品名字" align="center" prop="productName" width="120" />
      <el-table-column label="购买数量" align="center" prop="productCount" width="120" />
      <el-table-column label="单价" align="center" prop="productPrice" width="120" />
      <el-table-column label="提成" align="center" prop="productCommission" width="120" />
      <el-table-column label="总价" align="center" prop="totalAmount" width="120" />
      <el-table-column label="支付状态" align="center" prop="payStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhifu" :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="支付状态 0未支付 1支付中 2支付成功 3支付失败 9取消支付" align="center" prop="payStatus" /> -->
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付类型" align="center" prop="payType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zftype" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="支付类型 0平台支付 1微信支付 2余额支付" align="center" prop="payType" /> -->
      <el-table-column label="收货人" align="center" prop="deliveryRecipientName" width="120" />
      <el-table-column label="收货人电话" align="center" prop="deliveryRecipientPhone" width="120" />
      <el-table-column label="省" align="center" prop="deliveryProvince" width="120" />
      <el-table-column label="市" align="center" prop="deliveryCity" width="120" />
      <el-table-column label="区县" align="center" prop="deliveryCounty" width="120" />
      <el-table-column label="详细地址" align="center" prop="deliveryAddress" width="120" />
      <el-table-column label="发货状态" align="center" prop="deliveryStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fhzt" :value="scope.row.deliveryStatus" />
        </template>
      </el-table-column>
      <el-table-column label="发货类型" align="center" prop="deliveryLogisticsType" width="120">
        <template slot-scope="scope">
            <el-tag v-if="scope.row.deliveryLogisticsType == 2" type="success">同城配送</el-tag>
            <el-tag v-if="scope.row.deliveryLogisticsType == 3" type="success">虚拟发货</el-tag>
            <el-tag v-if="scope.row.deliveryLogisticsType == 4" type="success">用户自提</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="快递平台" align="center" prop="deliveryName" width="120" />
      <el-table-column label="快递编号" align="center" prop="deliveryNumber" width="120" />
      <el-table-column label="发货人名字" align="center" prop="deliveryUserName" width="120" />
      <el-table-column label="发货时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createUserName" width="120" />
      <el-table-column label="创建人身份" align="center" prop="createUserRole" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['orders:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="fahuo(scope.row)"
            v-hasPermi="['orders:delivery']">发货</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog title="发货" :visible.sync="opens" width="600px" append-to-body>
      <el-form ref="form" :model="forms" :rules="rules" label-width="120px">
        <el-form-item label="快递平台名字" prop="deliveryName">
          <el-input v-model="forms.deliveryName" placeholder="请输入快递平台名字" />
        </el-form-item>
        <el-form-item label="快递编号" prop="deliveryNumber">
          <el-input v-model="forms.deliveryNumber" placeholder="请输入快递编号" />
        </el-form-item>
        <el-form-item label="发货类型" prop="deliveryLogisticsType">
          <el-radio-group v-model="forms.deliveryLogisticsType">
            <el-radio :label="2">同城配送</el-radio>
            <el-radio :label="3">虚拟发货</el-radio>
            <el-radio :label="4">用户自提</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="收货人" prop="deliveryRecipientName">
          <el-input v-model="form.deliveryRecipientName" placeholder="请输入收货人" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="deliveryRecipientPhone">
          <el-input v-model="form.deliveryRecipientPhone" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="省" prop="deliveryProvince">
          <el-input v-model="form.deliveryProvince" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="deliveryCity">
          <el-input v-model="form.deliveryCity" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="区县" prop="deliveryCounty">
          <el-input v-model="form.deliveryCounty" placeholder="请输入区县" />
        </el-form-item>
        <el-form-item label="详细地址" prop="deliveryAddress">
          <el-input v-model="form.deliveryAddress" placeholder="请输入详细地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrders,
  getOrders,
  delOrders,
  addOrders,
  updateOrders,
  updatedelivery, getlistsss
} from "@/api/system/dingdan";
export default {
  name: "Orders",
  dicts: ["xiadanleixing", "zhifu", "zftype", "fhzt"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      zflx:[
      {
          id: 2,
          name: "同城配送",
        },
        {
          id: 3,
          name: "虚拟发货",
        },
        {
          id: 4,
          name: "用户自提",
        },
      ],
      zf: [
        {
          id: 2,
          name: "支付成功",
        },
        {
          id: 3,
          name: "支付失败",
        },
        {
          id: 0,
          name: "未支付",
        },
      ],
      fhstatus: [
        {
          id: 0,
          name: "未发货",
        },
        {
          id: 1,
          name: "已发货",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createUserRole: null,
        deliveryNumber: null,
        deliveryRecipientName: null,
        deliveryRecipientPhone: null,
        deliveryStatus: null,
        orderChannel: null,
        orderNumber: null,
        payStatus: null,
        productName: null,
        shopName: null,
        deliveryLogisticsType:null
      },
      // 表单参数
      form: {},
      forms: {},
      // 表单校验
      rules: {
        deliveryName: [
          { required: true, message: "快递平台名字不能为空", trigger: "blur" },
        ],
        deliveryNumber: [
          { required: true, message: "快递编号不能为空", trigger: "blur" },
        ],
        deliveryLogisticsType: [
          { required: true, message: "发货类型不能为空", trigger: "blur" },
        ],
      },
      jslist: [
        {
          roleName: '合作店',
          roleKey: "hezuodian"
        },
        {
          roleName: '平台代理商',
          roleKey: "dailishang"
        },
        {
          roleName: '平台客服',
          roleKey: "kefu"
        }
      ],
      daililist: []
    };
  },
  created() {
    this.getList();
    // getUser().then((response) => {
    //   this.jslist = response.roles;
    // });
    getlistsss({ codes: 'dailishang' }).then(res => {
      this.daililist = res.data
    })
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then((response) => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        orderChannel: null,
        shopId: null,
        shopName: null,
        shopTypeId: null,
        shopTypeName: null,
        shopPerson: null,
        shopPhone: null,
        productId: null,
        productName: null,
        productCount: null,
        productPrice: null,
        productCommission: null,
        totalAmount: null,
        payStatus: null,
        payTime: null,
        payType: null,
        deliveryRecipientName: null,
        deliveryRecipientPhone: null,
        deliveryProvince: null,
        deliveryCity: null,
        deliveryCounty: null,
        deliveryAddress: null,
        deliveryStatus: null,
        deliveryName: null,
        deliveryNumber: null,
        deliveryUserId: null,
        deliveryUserName: null,
        deliveryTime: null,
        createUserId: null,
        createUserName: null,
        createUserRole: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updatedelivery(this.forms).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("发货成功");
              this.getList();
            }
            this.opens = false;
          });
        }
      });

    },
    fahuo(row) {
      this.reset();
      // const id = row.id || this.ids
      // let data = {
      //     id: row.id
      // }
      console.log(row);
      this.forms = {
        id: row.id,
        deliveryName: "",
        deliveryNumber: "",
      };
      this.opens = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      let data = {
        id: row.id,
      };
      getOrders(data).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrders(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/orders/export",
        {
          ...this.queryParams,
        },
        `订单记录${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
